package com.fq.ioc.utils;

import java.lang.reflect.Field;

/**
 * @author jifang
 * @since 16/1/18下午10:16.
 */
public class SimpleValueSetUtils {

    public static void setSimpleValue(Field field, Object targetObject, String value) throws IllegalAccessException {
        Class<?> fieldType = field.getType();
        if (fieldType == String.class) {
            field.set(targetObject, value);
        } else if (fieldType == int.class || fieldType == Integer.class) {
            field.set(targetObject, Integer.parseInt(value));
        } else if (fieldType == double.class || fieldType == Double.class) {
            field.set(targetObject, Double.parseDouble(value));
        } else if (fieldType == float.class || fieldType == Float.class) {
            field.set(targetObject, Float.parseFloat(value));
        } else if (fieldType == byte.class || fieldType == Byte.class) {
            field.set(targetObject, Byte.parseByte(value));
        } else if (fieldType == short.class || fieldType == Short.class) {
            field.set(targetObject, Short.parseShort(value));
        } else if (fieldType == long.class || fieldType == Long.class) {
            field.set(targetObject, Long.parseLong(value));
        } else if (fieldType == char.class || fieldType == Character.class) {
            if (value.length() != 1) {
                throw new RuntimeException("不是有效的char类型");
            }
            field.set(targetObject, value.charAt(0));
        } else if (fieldType == boolean.class || fieldType == Boolean.class) {
            field.set(targetObject, Boolean.parseBoolean(value));
        } else {
            throw new RuntimeException("不是有效的value类型");
        }
    }
}
